package com.ssyt.hiringtwo.action.pub;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.google.code.kaptcha.Constants;
import com.ssyt.hiringtwo.action.BaseAction;
import com.ssyt.hiringtwo.common.StringUtil;
import com.ssyt.hiringtwo.entity.CompanyEntity;
import com.ssyt.hiringtwo.entity.PageModel;
import com.ssyt.hiringtwo.entity.PositionEntity;
import com.ssyt.hiringtwo.entity.UserEntity;
import com.ssyt.hiringtwo.entity.dtoTrade.Trade;
import com.ssyt.hiringtwo.entity.emailUtil.MailUtil;
import com.ssyt.hiringtwo.servies.pub.UserService;

public class PubAction extends BaseAction {
	private UserEntity user;
	/* 创建业务层的对象 */
	private UserService userService = new UserService();
	private String msg;// 错误提示语句
	private List<PositionEntity> latestPostionList;// 查询position最新的职位
	private List<Trade> tradelist;
	private PositionEntity position;
	private PositionEntity positionEntity;
	private PageModel<PositionEntity> pageModel;
	// --------------------------------------
	private String name;// 职位名称查询
	private String newpasswordH;// 新密码
	private String oldpassword;// 原密码
	private String validateCode;// 输入验证码
	private String newEmail;// 输入的邮箱
	// ----------------------------
	private static final String USERNAME = "1301394248@qq.com";
	private String newps;

	/**
	 * 转到登录的页面
	 */
	public String tologinUI() {
		return "tologinUI";
	}

	/**
	 * 跳转到主页
	 */
	public String toIndexUI() {
		// 查询首页上的行业相关信息
		tradelist = userService.ListTreade();
		// 查询最新职业信息(+职位条件查询)
		latestPostionList = userService.latestPositionListService();
		return "toIndexUI";
	}

	/**
	 * 登录的方法
	 */
	public String login() {
		// 根据邮箱和密码判断用户是否存在
		UserEntity userEntity = userService.login(user);
		if (userEntity == null) {
			// 如果用户不存在跳转到登录页面
			msg = "账号和密码错误!";
			return "tologinUI";

		} else {
			// 判断是有效账号，，还是无效账号
			if (userEntity.getIsActive() == UserEntity.ACCOUNT_VALID) {
				HttpSession session = ServletActionContext.getRequest()
						.getSession();
				session.setAttribute("userEntity", userEntity);

				// 判断，，如果是求职者，判断姓名电话填写没有
				if (userEntity.getRoleType() == UserEntity.ROLE_TALENT) {
					String username = userEntity.getUsername();
					String phone = userEntity.getPhone();
					if (StringUtil.isBlank(username)
							|| StringUtil.isBlank(phone)) {
						return "toBindInfoUI";
					} else {
						// 注册了就跳转主页
						return toIndexUI();
					}
				}// 判断，，如果是公司就跳转公司页面
				else if (userEntity.getRoleType() == UserEntity.ROLE_COMPANY) {
					CompanyEntity company = userService.getCompanInfo(userEntity.getId());
					if (company != null) {
						return "toCompanyUI";
					} else {

						return "toCompanyInfoUI";
					}

				} else {
					// 其他就是管理员
					return "toAdminUI";
				}
			} else {
				msg = "此账号未激活!请查看邮件激活账号!";
				return "tologinUI";
			}

		}
	}

	/**
	 * 注销login
	 * 
	 * @return
	 */
	public String logout() {
		getSession().invalidate();
		return toIndexUI();
	}
	/**
	 * 主页搜索框条件查询
	 */
	public String indexquery(){
		// 查询首页上的行业相关信息
		tradelist = userService.ListTreade();
		latestPostionList = userService.latestPositionListServicequery(positionEntity);
		return "toIndexUI";
	}
	
	
	
	
	/**
	 * 账号注册初始化页面
	 */
	public String registered() {
		return "registered";
	}

	/**
	 * 注册成功
	 * 
	 * @return
	 */
	public String registeredSuccess() {
			int row = userService.registeredSuccessService(user);
			if (row!=0) {
				if (StringUtil.isNotBlank(user.getEmail())) {
					StringBuffer sb = new StringBuffer();
					sb.append("<p>你好<a href=\"mailto:" + USERNAME	+ "\"target=\"_blank\">" + USERNAME + "</a>!</p>");
					sb.append("<p>你已经请求了激活账号,可以点击下面的连接来激活账号</p>");
					sb.append("<p><a href=\"http://localhost:8080/hiringtwo/pub/Pub/isactivation.action?user.email="+user.getEmail() + "\" target=\"_blank\">点击跳转(5分钟后无效) </a></p>");
					sb.append("<p>如果你没有请求重置密码,请忽略这封邮件</p>");
					sb.append("<p>在你点击上面连接激活之前,你的账号登陆不进去.</p>");
					MailUtil.send(USERNAME, "hiring激活账号信息", sb.toString());
				}else{
					System.out.println("修改失败");
				}
			}
			
			
		return "registeredSuccessUI";

	}

	/**
	 * 注册激活页面isactivation
	 */
	public String isactivation() {
		return "isactivation";
	}

	/**
	 * 初始化激活页面修改状态
	 */
	public String isactivationtwo() {
		userService.isactivation(user);
		return "tologinUI";
	}
	
	
	

	/**
	 * 初始化首页职位管理页面(查看更多职位)
	 * 
	 * @return
	 */
	public String CompanyPositionSearch() {
		if(null==positionEntity){
			positionEntity=new PositionEntity();
		}
		if(null==pageModel){
			pageModel=new PageModel<>(5,1);
		}
		pageModel = userService.list(positionEntity, 5, pageModel.getPageNo(), positionEntity.getName());
		return "toCompanyPositionSearchUI";

	}

	/**
	 * 初始化公司职位详情( position-Details.jsp)
	 * 
	 * @return
	 */
	public String PositionDetails() {
		// 查询职位和公司
		position = userService.positionDetails(position.getId());
		// 查询相似职位(position.getName()根据名字模糊查询,position.getId()相似职位不能取已经显示了的)
		latestPostionList = userService.SimilarPosition(position.getName(),
				position.getId());
		return "toPositionDetailsUI";
	}

	/**
	 * 初始化修改密码界面
	 * 
	 * @return
	 */
	public String CompanyResetPassword() {
		// session中获取求职者信息getTalent()
		UserEntity userEntity = getTalent();
		String password = userEntity.getPassword();// session里拿出原密码
		boolean correct = password.equals(this.oldpassword);// 输入的原密码和session里面的密码匹配

		if (correct) {
			// 执行修改密码功能
			int id = userEntity.getId();
			int row = userService.updatePassword(id, newpasswordH);
			if (row != 1) {
				msg = "修改失败！！";
			} else {
				// 成功过后要把session里存的password更新
				userEntity.setPassword(newpasswordH);
				return "tologinUI";
			}
		} else {
			msg = "修改失败！！";
		}

		return "toCompanyResetPasswordUI";
	}

	/**
	 * 初始化求职者简历界面
	 * 
	 * @return
	 */
	public String resumeListUI() {

		return "toresumeListUI";
	}

	/**
	 * 初始化忘记密码页面
	 * 
	 * @return
	 */
	public String resetPasswordByEsmil() {
		String rand = (String) getSession().getAttribute(
				Constants.KAPTCHA_SESSION_KEY);
		if (StringUtil.isNotBlank(validateCode)) {

			boolean equals = rand.equals(this.validateCode);
			// 1.判断验证码
			if (equals) {
				// 2.检查邮箱中数据库是否存在
				boolean existEmail = userService.isExistEmail(newEmail);
				if (existEmail) {
					// 3.如果验证码正确，在执行邮件发送
					StringBuffer sb = new StringBuffer();
					sb.append("<p>你好<a href=\"mailto:" + USERNAME
							+ "\"target=\"_blank\">" + USERNAME + "</a>!</p>");
					sb.append("<p>你已经请求了重置密码,可以点击下面的连接来重置密码</p>");
					sb.append("<p><a href=\"http://localhost:8080/hiringtwo/pub/Pub/resetPassword.action?newEmail=" + USERNAME+ "\"target=\"_blank\">点击跳转(5分钟后无效) </a></p>");
					sb.append("<p>如果你没有请求重置密码,请忽略这封邮件</p>");
					sb.append("<p>在你点击上面连接修改密码之前,你的密码将会保持不变.</p>");
					MailUtil.send(newEmail, "hiring重置密码信息", sb.toString());

					// ------------收件人-------主题-----------内容
				} else {
					msg = "账号不匹配!";
				}

			} else {
				msg = "验证码不匹配!";
				return "toresetPasswordByEsmilUI";
			}

		}
		return "toresetPasswordByEsmilUI";
	}

	/**
	 * 初始化重置密码界面
	 * 
	 * @return
	 */
	public String resetPassword() {

		if (StringUtil.isNotBlank(newps)) {
			int row = userService.updateEmailPassword(newEmail, newps);
			if (row > 0) {
				return "tologinUI";
			}
		}

		return "toresetPasswordUI";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<PositionEntity> getLatestPostionList() {
		return latestPostionList;
	}

	public void setLatestPostionList(List<PositionEntity> latestPostionList) {
		this.latestPostionList = latestPostionList;
	}

	public List<Trade> getTradelist() {
		return tradelist;
	}

	public void setTradelist(List<Trade> tradelist) {
		this.tradelist = tradelist;
	}

	public PositionEntity getPosition() {
		return position;
	}

	public void setPosition(PositionEntity position) {
		this.position = position;
	}

	public PageModel<PositionEntity> getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel<PositionEntity> pageModel) {
		this.pageModel = pageModel;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getNewpasswordH() {
		return newpasswordH;
	}

	public void setNewpasswordH(String newpasswordH) {
		this.newpasswordH = newpasswordH;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public String getNewEmail() {
		return newEmail;
	}

	public void setNewEmail(String newEmail) {
		this.newEmail = newEmail;
	}

	public String getNewps() {
		return newps;
	}

	public void setNewps(String newps) {
		this.newps = newps;
	}

	public PositionEntity getPositionEntity() {
		return positionEntity;
	}

	public void setPositionEntity(PositionEntity positionEntity) {
		this.positionEntity = positionEntity;
	}



	public static String getUsername() {
		return USERNAME;
	}


	

}
